<template>
  <div class="app-container">
    <el-card style="border-radius: 10px">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="85px">

        <el-form-item label="工序编码" prop="processCode">
          <el-input
              v-model="queryParams.processCode"
              placeholder="请输入工序编码"
              clearable
              @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="工序名称" prop="processName">
          <el-input
              v-model="queryParams.processName"
              placeholder="请输入工序名称"
              clearable
              @keyup.enter.native="handleQuery"
          />
        </el-form-item>

        <el-form-item label="工作站编码" prop="workstationCode">
          <el-input
              v-model="queryParams.workstationCode"
              placeholder="请输入工作站编码"
              clearable
              @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="工作站名称" prop="workstationName">
          <el-input
              v-model="queryParams.workstationName"
              placeholder="请输入工作站名称"
              clearable
              @keyup.enter.native="handleQuery"
          />
        </el-form-item>

        <el-form-item label="设备编码" prop="machineryCode">
          <el-input
              v-model="queryParams.machineryCode"
              placeholder="请输入设备编码"
              clearable
              @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="设备名称" prop="machineryName">
          <el-input
              v-model="queryParams.machineryName"
              placeholder="请输入设备名称"
              clearable
              @keyup.enter.native="handleQuery"
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
    </el-card>

    <el-card style="border-radius: 10px">
      <el-table v-loading="loading" :data="wageStandardList" border>
        <el-table-column label="工序编码" align="center" prop="processCode">
          <template slot-scope="scope">
            <el-button
                type="text"
                @click="handleView(scope.row)"
                v-hasPermi="['hr:wageStandard:list']"
            >{{scope.row.processCode}}</el-button>
          </template>
        </el-table-column>
        <el-table-column label="工序名称" align="center" prop="processName" />
        <el-table-column label="工作站编码" align="center" prop="workstationCode" />
        <el-table-column label="工作站名称" align="center" prop="workstationName" />
        <el-table-column label="设备编码" align="center" prop="machineryCode" />
        <el-table-column label="设备名称" align="center" prop="machineryName" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
                size="mini"
                type="text"
                @click="handleList(scope.row)"
                v-hasPermi="['hr:wageStandard:list']"
            >工资标准</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
      />
    </el-card>

    <!-- 添加或修改计价工资标准对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="85px">
        <el-card style="border-radius: 10px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="工序ID" prop="processId" style="display: none;">
                <el-input v-model="form.processId" placeholder="请输入工序ID" />
              </el-form-item>
              <el-form-item label="工序编码" prop="processCode">
                <el-input v-model="form.processCode" placeholder="请输入工序编码" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="工序名称" prop="processName">
                <el-input v-model="form.processName" placeholder="请输入工序名称" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="工作站ID" prop="workstationId" style="display: none;">
                <el-input v-model="form.workstationId" placeholder="请输入工作站ID" />
              </el-form-item>
              <el-form-item label="工作站编码" prop="workstationCode">
                <el-input v-model="form.workstationCode" placeholder="请输入工作站编码" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="工作站名称" prop="workstationName">
                <el-input v-model="form.workstationName" placeholder="请输入工作站名称" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="设备ID" prop="machineryId" style="display: none;">
                <el-input v-model="form.machineryId" placeholder="请输入设备ID" />
              </el-form-item>
              <el-form-item label="设备编码" prop="machineryCode">
                <el-input v-model="form.machineryCode" placeholder="请输入设备编码" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="设备名称" prop="machineryName">
                <el-input v-model="form.machineryName" placeholder="请输入设备名称" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-card>

        <el-card style="border-radius: 10px">
          <WageStandardLine
              ref="wageStandardLine"
              :processId="form.processId"
              :workstationId="form.workstationId"
              :machineryId="form.machineryId"
              :optType="optType"
          >
          </WageStandardLine>
        </el-card>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="optType !== 'view'">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getList } from "@/api/mes/hr/wageStandard";
import WageStandardLine from "./line.vue"

export default {
  name: "WageStandard",
  components: {
    WageStandardLine
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 计价工资标准表格数据
      wageStandardList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        processId: null,
        processCode: null,
        processName: null,
        workstationId: null,
        workstationCode: null,
        workstationName: null,
        machineryId: null,
        machineryCode: null,
        machineryName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      optType: null,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询计价工资标准列表 */
    getList() {
      this.loading = true;
      getList(this.queryParams).then(response => {
        this.wageStandardList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        processId: null,
        processCode: null,
        processName: null,
        workstationId: null,
        workstationCode: null,
        workstationName: null,
        machineryId: null,
        machineryCode: null,
        machineryName: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 打开工资标准页面 **/
    handleList(row) {
      this.reset();
      if (row.processId == null) {
        this.$modal.msgError("请检查工序配置！")
        return
      }
      this.form.processId = row.processId;
      this.form.processCode = row.processCode;
      this.form.processName = row.processName;
      if (row.workstationId == null) {
        this.$modal.msgError("请先给工序配置工作站！")
        return
      }
      this.form.workstationId = row.workstationId;
      this.form.workstationCode = row.workstationCode;
      this.form.workstationName = row.workstationName;
      if (row.machineryId == null) {
        this.$modal.msgError("请先配置工作站设备！")
        return
      }
      this.form.machineryId = row.machineryId;
      this.form.machineryCode = row.machineryCode;
      this.form.machineryName = row.machineryName;
      this.open = true;
      this.title = "工资标准设置";
      this.optType = 'add'
    },
    // 查看按钮操作
    handleView(row) {
      this.reset();
      this.form.processId = row.processId;
      this.form.processCode = row.processCode;
      this.form.processName = row.processName;
      this.form.workstationId = row.workstationId;
      this.form.workstationCode = row.workstationCode;
      this.form.workstationName = row.workstationName;
      this.form.machineryId = row.machineryId;
      this.form.machineryCode = row.machineryCode;
      this.form.machineryName = row.machineryName;
      this.open = true;
      this.title = "查看工资标准";
      this.optType = 'view'
    },
    /** 提交按钮 */
    submitForm() {
      this.open = false;
      this.reset();
      this.$modal.msgSuccess("设置成功！")
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('hr/wageStandard/export', {
        ...this.queryParams
      }, `wageStandard_${new Date().getTime()}.xlsx`)
    },
  }
};
</script>
